package com.ruoyi.web.controller.system;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SyDatasetInfo;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SyDataset;
import com.ruoyi.system.service.ISyDatasetService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据集信息表Controller
 * 
 * @author SYGIS
 * @date 2023-12-22
 */
@RestController
@RequestMapping("/system/datasetinfo")
public class SyDatasetController extends BaseController {

    @Autowired
    private ISyDatasetService syDatasetService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysUserService userService;

    @PostMapping("/getCode")
    public AjaxResult getCode(@RequestBody SyDataset dataset){
        String code = getFirstSpell(dataset.getName());
        Map<Object,Object> res = new HashMap<>();
        res.put("code",code);
        SyDataset param = new SyDataset();
        param.setCode(code);
        List<SyDataset> list = syDatasetService.selectSyDatasetList(param);
        if(list.size()>0){
            return AjaxResult.error("数据集名称重复");
        }else{
            return AjaxResult.success(res);
        }
    }

    public static String getFirstSpell(String chinese) {
        StringBuffer pybf = new StringBuffer();
        char[] arr = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 128) {
                try {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
                    if (temp != null) {
                        pybf.append(temp[0].charAt(0));
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pybf.append(arr[i]);
            }
        }
        return pybf.toString().replaceAll("\\W", "").trim().toUpperCase();
    }

    /**
     * 查询数据集信息表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SyDataset syDataset) {
        startPage();
        List<SyDataset> list = syDatasetService.selectSyDatasetList(syDataset);
        if(list.size()>0){
            for(SyDataset dataset:list){
                SysUser user = userService.selectUserById(Long.parseLong(dataset.getCreateBy()));
                if(user!=null){
                    dataset.setCreator(user.getNickName());
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出数据集信息表列表
     */
    @Log(title = "数据集信息表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SyDataset syDataset) {
        List<SyDataset> list = syDatasetService.selectSyDatasetList(syDataset);
        ExcelUtil<SyDataset> util = new ExcelUtil<SyDataset>(SyDataset.class);
        util.exportExcel(response, list, "数据集信息表数据");
    }

    /**
     * 获取数据集信息表详细信息
     */
    @GetMapping(value = "/{uuid}")
    public AjaxResult getInfo(@PathVariable("uuid") String uuid) {
        return success(syDatasetService.selectSyDatasetByUuid(uuid));
    }

    /**
     * 新增数据集信息表
     */
    @Log(title = "数据集信息表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SyDataset dataset) {
        dataset.setUuid(UUID.randomUUID().toString().replaceAll("-", ""));
        dataset.setCreateBy(SecurityUtils.getLoginUser().getUserId().toString());
        dataset.setCreateTime(new Date());
        SysDept dept = deptService.selectDeptById(SecurityUtils.getLoginUser().getUser().getDeptId());
        if(dept!=null){
            dataset.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
            dataset.setDeptName(dept.getDeptName());
        }
        return toAjax(syDatasetService.insertSyDataset(dataset));
    }

    /**
     * 修改数据集信息表
     */
    @Log(title = "数据集信息表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SyDataset syDataset) {
        return toAjax(syDatasetService.updateSyDataset(syDataset));
    }

    /**
     * 删除数据集信息表
     */
    @Log(title = "数据集信息表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{uuids}")
    public AjaxResult remove(@PathVariable String[] uuids) {
        return toAjax(syDatasetService.deleteSyDatasetByUuids(uuids));
    }
}
